package com.lzw.mall.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.lzw.mall.admin.entity.User;
import com.lzw.mall.admin.mapper.UserMapper;
import com.lzw.mall.admin.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzw.mall.admin.util.RequestUtil;
import com.lzw.mall.common.util.StringUtil;
import com.lzw.mall.common.exception.BusinessException;
import com.lzw.mall.common.vo.LayuiTableVO;
import com.lzw.mall.admin.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lizhenwei
 * @since 2023-03-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public Boolean check(String userName, String password) {


        Object obj=redisTemplate.opsForValue().get("err"+userName);
        if(obj!=null){
            if((int)obj==5){
                //redisTemplate.delete("err"+userName);
                throw new BusinessException("密码连续输入错误五次，2小时之后再试");
            }
        }

        User user=userMapper.selectByUsername(userName);

        Boolean b= user!=null && user.getPassword().equals(StringUtil.md5(password));
        if(b) {
            RequestUtil.setCurrent(user);
            redisTemplate.delete("err"+userName);

        }else {
            if(obj!=null){
                //(int)obj+1
                redisTemplate.opsForValue().set("err"+userName,(int)obj+1,2, TimeUnit.HOURS);
            }else {
                redisTemplate.opsForValue().set("err"+userName,1,2, TimeUnit.HOURS);
            }

            //throw new BusinessException("密码输入错误请重新输入");
        }
        //System.out.println("b"+b);
        return b;
    }

    @Override
    public LayuiTableVO list(Integer page, Integer limit, String keyword,String sex) {
        PageHelper.startPage(page,limit);
        return LayuiTableVO.data(userMapper.selectListPage(keyword,sex));
    }

    @Override
    public ResultVO doSave(User u) {
        ResultVO vo=new ResultVO();
        if (u.getId() == null) {//新增
            if(!StringUtil.isNotBlank(u.getUsername())){
                throw new BusinessException("用户名不能为空！") ;
            }
            if(!StringUtil.isNotBlank(u.getPassword())){
                throw new BusinessException("密码不能为空") ;
            }
            User user =userMapper.selectByUsername(u.getUsername());
            if(user!=null){
                throw new BusinessException("用户名已经被使用 ") ;
            }
            u.setPassword(StringUtil.md5(u.getPassword()));
            vo.setMsg("新增用户成功");

            userMapper.insert(u);

            return vo;
        } else {
            Object obj=RequestUtil.getCurrent();
            User user=(User) obj;
            u.setPassword(StringUtil.md5(u.getPassword()));
            if(user.getUsername().equals(u.getUsername()) || user.getUsername().equals("admin")){
                List<User> list =userMapper.selectByUsernameAndId(u.getUsername(),u.getId());
                if(list.size()>0){
                    throw new BusinessException("用户名已经被使用 ") ;
                }
                userMapper.doUpdateById(u.getId(),u.getUsername(),u.getSex(),u.getName(),u.getPhone(),u.getAvatar());
                vo.setMsg("编辑成功");
                return vo;
            }else {
                throw new BusinessException("您不是超级管理员，不能修改他人信息") ;
            }
        }
    }
    @Override
    public ResultVO delete(Integer u) {
        ResultVO vo=new ResultVO();
        userMapper.deleteById(u);
        return vo;
    }

    @Override
    public ResultVO selectById(Integer id) {
        ResultVO vo=new ResultVO();
        vo.setCode(0);
        vo.setData(userMapper.doSelectById(id));
        return vo;
    }

    @Override
    public void deletes(Integer[] ids) {
        userMapper.deletes(ids);
    }

    @Override
    public ResultVO selectByUsername(String username) {
        ResultVO vo=new ResultVO();
        vo.setData(userMapper.selectByUsername(username));
        return vo;
    }

    @Override
    public ResultVO updatePassword(String oldPassword, String newPassword, String againPassword) {
        ResultVO vo=new ResultVO();
        User u=userMapper.selectByUsername(RequestUtil.getCurrent().getUsername());
        String p=u.getPassword();
        Integer id=u.getId();
        if (p.equals(StringUtil.md5(oldPassword))) {
            if (newPassword.equals(againPassword)){
                userMapper.updatePassword(StringUtil.md5(newPassword),id);
                vo.setMsg("修改成功，请重新登录！");
            }else {
                throw new BusinessException("输入的两次密码不一致！请重新输入");
            }
        }else {
            throw new BusinessException("原密码输入错误！请重新输入");
        }
        return vo;
    }

    @Override
    public ResultVO updateMima(Integer id) {
        ResultVO vo=new ResultVO();
        userMapper.updateMima(id);
        vo.setMsg("重置成功！初始密码为123456");
        return vo;
    }
}
